package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.Complaint;
import chuishop.yiqimaicai.service.ComplaintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ComplaintController {


    @Autowired
    private ComplaintService complaintService;

    @RequestMapping("findAllComplaint")
    @ResponseBody
    public PageInfo<Complaint> findAllComplaint(Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Complaint> list=complaintService.findAllComplaint();
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i).getCid()+"\t"+list.get(i).getUid()+"\t"
                    +list.get(i).getUname()+"\t" +list.get(i).getCreason() );
        }
        PageInfo<Complaint> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("findComplaintById")
    @ResponseBody
    public PageInfo<Complaint> findComplaintById(Integer cid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Complaint> list=complaintService.findComplaintById(cid);

        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i).getCid()+"\t"+list.get(i).getUid()+"\t"
                    +list.get(i).getUname()+"\t" +list.get(i).getCreason() );
        }
        PageInfo<Complaint> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    //删除功能
    @RequestMapping("delComplaint")
    @ResponseBody
    public Map<Object, Object> delComplaint(String ids) {
        System.out.println(ids);

        String idstr[] = ids.split(",");
        for (String str : idstr) {
            System.out.println(str);
        }
        System.out.println(idstr.length);

        Map<Object, Object> result = new HashMap<>();
        boolean flag = complaintService.delComplaint(idstr);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "删除成功");
        } else {
            result.put("status", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }

    @RequestMapping("addComplaint")
    @ResponseBody
    public Map<Object, Object> addComplaint(@RequestBody Complaint complaint) {
        System.out.println(complaint.getCid()+"\t"+complaint.getUid()+"\t"+complaint.getUname()+"\t"+complaint.getCreason());
        Map<Object, Object> result = new HashMap<>();
        boolean flag =complaintService.addComplaint(complaint);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    @RequestMapping("updateComplaint")
    @ResponseBody
    public Map<Object, Object> updateComplaint(@RequestBody Complaint complaint) {
        System.out.println(complaint.getCid()+"\t"+complaint.getUid()+"\t"+complaint.getUname()+"\t"+complaint.getCreason());
        Map<Object, Object> result = new HashMap<>();
        boolean flag =complaintService.updateComplaint(complaint);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }
}
